#include "head.h"

// 单链表的创建
U_list* S_List_init()
{
    // 创建头节点
    U_list *list = (U_list *)malloc(sizeof(U_list));
    if (list == NULL)
    {
        ERRLOG("创建动态空间失败");
        return NULL;
    }
    //2、初始化赋值
    list->next = NULL;
    //3、返回list
    return list;
}

// 单链表的插入
void S_List_insert(U_list* list, U_data data, int pos)
{
    if (list == NULL)
    {
        printf("单链表为NULL\n");
        return;
    }
    U_node * new_node = (U_node *)malloc(sizeof(U_node));
    if (new_node == NULL)
    {
        printf("new_node创建失败\n");
        return;
    }
    new_node->next = NULL;
    new_node->data = data;

    if (pos == 1)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }
    U_node *ptr = list->next;
    for (size_t i = 1; i < pos - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }
    if (ptr == NULL)
    {
        printf("ptr已经指向null\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;
    return;
}

// 单链表的删除
void S_List_delete(U_list *list, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node *ptr_1 = list->next;
    U_node *ptr_2 = NULL;
    if (pos == 1)
    {
        list->next = ptr_1->next;
        free(ptr_1);
        return;
    }
    for (size_t i = 0; i < pos-1 && ptr_1->next != NULL; i++)
    {
        ptr_2 = ptr_1;
        ptr_1 = ptr_1->next;
    }
    if (ptr_1 == NULL)
    {
        printf("ptr_1指向null\n");
        return;
    }
    ptr_2->next = ptr_1->next;
    free(ptr_1);
    return;
}

// 单链表的名字删除
void S_List_delete_name(U_list *list,char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node * ptr = list->next;
    for (size_t i = 0; ptr != NULL; i++)
    {
        if (strcmp(ptr->data.name,name) == 0)
        {
            S_List_delete(list,i+1);
            return;
        }
        ptr = ptr->next;
    }
    return;
}

// 根据名字返回位置
int by_name_to_pos(U_list *list,char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return EOF;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return EOF;
    }
    int pos = 1;
    U_node * ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name,name) == 0)
        {
            return pos;
        }
        ptr = ptr->next;
        pos++;
    }
    return 0;
}

// 单链表的查找
U_node* S_List_find_name(U_list* list, char* name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }
    U_node * ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name,name) == 0)
        {
            return ptr;
        }
        ptr = ptr->next;
    }
    return NULL;
}

// 单链表的遍历
void S_List_print(U_list* list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        printf("用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;
    }
}

// 单链表的清空
void S_List_clear(U_list* list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node *ptr_1 = list->next;
    U_node *ptr_2 = ptr_1->next;
    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    list->next = NULL;
}

// 单链表的合并
U_list* S_List_merge(U_list* list1, U_list* list2)
{
    if (list1 == NULL || list2 == NULL)
    {
        printf("链表不存在\n");
        return list1;
    }
    //求链表list1的链表长度
    U_node *ptr_1 = list1->next;
    int len = 0;
    while (ptr_1 != NULL)
    {
        ptr_1 = ptr_1->next;
        len++;
    }
    ptr_1 = list1->next;
    U_node *ptr_2 = list2->next;
    while (ptr_2 != NULL)
    {
        len++;
        S_List_insert(list1,ptr_2->data,len);
        ptr_2 = ptr_2->next;
    }
    return list1;
}

// 单链表的销毁
void S_List_free(U_list **list)
{
    if (list == NULL || *list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    U_node *ptr_1 = (*list)->next;
    U_node *ptr_2 = ptr_1->next;
    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    free(*list);
    *list = NULL;
}

// 向文件写入用户信息
void S_List_File_print(U_list *list, const char *file_name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    FILE *fp = fopen(file_name,"w+");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    fseek(fp,0,SEEK_SET);
    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        fprintf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
                ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;
    }
    //刷新
    fflush(fp);
    fclose(fp);
}

// 从文件读取用户信息
void S_List_File_scanf(U_list *list, const char *file_name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    FILE *fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    // 移动光标
    fseek(fp, 0, SEEK_SET);
    //清空链表
    S_List_clear(list);

    U_data data;
    char ch = 0;
    int pos = 0;
    //检测文件行数
    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            pos++;
        }
    }
    //移动光标
    fseek(fp, 0, SEEK_SET);
    for (size_t i = 0; i < pos; i++)
    {
        fscanf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               data.name, data.passwd, &data.grade, &data.value);
        S_List_insert(list, data, i + 1);
    }
    fflush(fp);
    fclose(fp);
}